package com.bc.bcsellerapihttp.common;

import com.bc.bccore.webconfig.WebConfig;
import com.bc.bcmodel.consts.GlobalSettings;
import com.bc.bcmodel.resp.UserCookieResp;
import com.bc.commoncore.security.AES;
import com.bc.commoncore.util.CookieUtils;
import com.bc.commoncore.util.DateUtil;
import com.bc.commoncore.util.JsonUtil;
import com.bc.commonentity.common.AjaxNotLoginRunTimeException;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashSet;
import java.util.regex.Pattern;

/**
 * @Name AuthInteceptor
 * @Description
 * @Author douhaichao
 * @Date 2018/8/17 22:56
 * @Version 1.0
 **/
public class AuthInteceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!needCheck(request)) {
            return true;
        }
        if (handler instanceof HandlerMethod) {
            // 判断是否登录
            if (WebConfig.getIsDebug() == 1) {
//                String user_id = "1";
//                user_id = AES.aesEncrypt(user_id);
//                //本地用户
//                CookieUtils.setCookie(request, response, GlobalSettings.SELLER_COOKIENAME, user_id);
                UserCookieResp u = new UserCookieResp();
                u.setLogin_time(DateUtil.getToday());
                u.setUser_id("U000032002");
                String js = JsonUtil.obj2string(u);
                js = AES.aesEncrypt(js);
                //本地用户
                CookieUtils.setCookie(request, response, GlobalSettings.SELLER_COOKIENAME, js);
            }
            UserContext context = UserContext.getCurrentContext();
//            boolean isLogin = context != null;
//            if (!isLogin) {
//                throw new AjaxNotLoginRunTimeException("请重新登录");
//                response.getWriter().write("<script>alert('Please log in and retry.');window.location='/';</script>");
//                return false;
//            }
        }
        return true;
    }

    public static boolean needCheck(HttpServletRequest request) {
        String url = request.getServletPath();
        HashSet<String> set = new HashSet();
        //白名单
        set.add("/api-docs");
        set.add("/alert/heart");
        set.add("/swagger");
        set.add("manage");
        set.add("/");
        if (set.contains(url)) {
            return false;
        }

        return !url.startsWith("/nologon/");
    }

//    private static boolean cfgPatterns(String url) {
//        String[] patternStrArr = {"/nologon/*"};
//        for (int i = 0; i< patternStrArr.length; i++) {
//            if (Pattern.compile(patternStrArr[i]).matcher(url).matches()) {
//                System.out.println("===================["+url+"]");
//                return true;
//            }
//        }
//
//        return false;
//    }
}
